/* Copyright (c) 2025 Beijing Semidrive Technology Corporation
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/********************************************************************************************************
 *  \file     Mcal_Delay.h                                                                                  *
 *  \brief    This file contains interface header for DMA MCAL driver.                                  *
 *                                                                                                      *
 * <table>                                                                                              *
 * <tr><th>Date           <th>Version                                                                   *
 * <tr><td>2025/01/06     <td>1.0.0                                                                     *
 * </table>                                                                                             *
 *******************************************************************************************************/
#ifndef MCAL_DELAY_H
#define MCAL_DELAY_H

#ifdef __cplusplus
extern "C" {
#endif
/********************************************************************************************************
 *                                      Include header files                                            *
 *******************************************************************************************************/
#include "Mcal_Types.h"
#include "Mcu_Soc.h"
/********************************************************************************************************
 *                                 Global Macro definition                                              *
 *******************************************************************************************************/
#ifndef MCAL_CPU_FREQUENCY_MHZ
#define MCAL_CPU_FREQUENCY_MHZ           (Mcu_Ip_SocGetCoreFreq())
#endif /* #ifndef MCAL_CPU_FREQUENCY_MHZ */
#define PMU_US_TO_TICK(us)      ((uint32)(((us) * (MCAL_CPU_FREQUENCY_MHZ/4000000U))/16U))
/********************************************************************************************************
 *                                  Global Function Declarations                                        *
 *******************************************************************************************************/
/** *****************************************************************************************************
 * \brief Read pmu counter tick
 *
 * \verbatim
 * Syntax             : uint32 read_pmccntr(void);
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Non reentrant
 *
 * Parameters (in)    : None
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : pmu counter
 *
 * Description        : Read pmu counter tick
 *
 * \endverbatim
 *******************************************************************************************************/
uint32 read_pmccntr(void);
/** *****************************************************************************************************
 * \brief This function is to enable PMU counter.
 *        This function can only called after Mcu_InitClock();
 *
 * \verbatim
 * Syntax             : void Mcal_PmuCounterInit
 *                      (
 *                          void
 *                      )
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : None.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : None
 *
 * Description        : This function is to enable PMU counter.
 * \endverbatim
 * Traceability       : None
 *******************************************************************************************************/
void Mcal_PmuCounterInit(void);
/** *****************************************************************************************************
 * \brief This function is convert us to ticks.
 *
 * \verbatim
 * Syntax             : uint32 Mcal_UsToTicks
 *                      (
 *                          uint32 us
 *                      )
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : us - How many us will be convert to ticks.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : The ticks is converted out.
 *
 * Description        : This function is convert us to ticks.
 * \endverbatim
 * Traceability       : None
 *******************************************************************************************************/
uint32 Mcal_UsToTicks(uint32 us);
/** *****************************************************************************************************
 * \brief This function is convert ticks to us.
 *
 * \verbatim
 * Syntax             : uint32 Mcal_TicksToUs
 *                      (
 *                          uint32 ticks
 *                      )
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : ticks - How many ticks will be convert to us.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : The time is converted out.
 *
 * Description        : This function is convert ticks to us.
 * \endverbatim
 * Traceability       : None
 *******************************************************************************************************/
uint32 Mcal_TicksToUs(uint32 ticks);
/** *****************************************************************************************************
 * \brief This udelay function based on performance monitor unit.
 *
 * \verbatim
 * Syntax             : Std_ReturnType Mcal_udelay
 *                      (
 *                          uint32 us
 *                      )
 *
 * Service ID[hex]    : None
 *
 * Sync/Async         : Synchronous
 *
 * Reentrancy         : Reentrant
 *
 * Parameters (in)    : us - How many us will be delayed.
 *
 * Parameters (inout) : None
 *
 * Parameters (out)   : None
 *
 * Return value       : E_OK: Delay is OK.
 *                      MCALLIB_E_PMU_CNT: PMU counter is disable.
 *                      MCALLIB_E_PARAMS: Input parameter is error.
 *                      MCALLIB_E_TIMEOUT: Read counter timeout.
 *
 * Description        : Delay time in us. max time is 21 s.
 * \endverbatim
 * Traceability       : None
 *******************************************************************************************************/
Std_ReturnType Mcal_udelay(uint32 us);
#ifdef __cplusplus
}
#endif
#endif /* MCAL_DELAY_H */
/* End of file */
